<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row class="float-left">
          <el-button type="success" @click="handleCreate"
            >&emsp;新增&emsp;</el-button
          >
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>
        </el-row>
        <el-form
          :inline="true"
          :model="listQuery"
          ref="dataList2"
          class="demo-form-inline float-right"
        >
          <el-form-item label="">
            <el-input
              type="text"
              v-model="listQuery.name"
              placeholder="请输入任务名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-select
              @clear="listQuery.type = undefined"
              filterable
              v-model="listQuery.type"
              placeholder="请选择所属模块"
              clearable
            >
              <el-option
                v-for="item in TaskOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="searchSubmit">查询</el-button>
            <el-button type="primary" @click="handleResetSubmit"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList"
          @selection-change="handleSelectionChange"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column prop="id" label="序号"> </el-table-column>
          <el-table-column prop="name" label="任务名称"> </el-table-column>
          <el-table-column prop="task_type_name" label="任务类型">
          </el-table-column>
          <el-table-column prop="open_day" label="开放时间"> </el-table-column>
          <el-table-column prop="target_limit" label="条件值">
          </el-table-column>
          <el-table-column prop="cycle_days" label="循环周期">
          </el-table-column>
          <el-table-column prop="reward_name" label="赠送奖品">
          </el-table-column>
          <el-table-column prop="reward_number" label="奖品数量">
          </el-table-column>
          <el-table-column prop="reward_type" label="奖品类型">
            <template slot-scope="scope">
              <p v-if="scope.row.reward_type === 1">道具包</p>
              <p v-if="scope.row.reward_type === 2">道具</p>
            </template>
          </el-table-column>
          <el-table-column prop="desc" label="任务说明"> </el-table-column>
          <el-table-column prop="task_module_name" label="任务模块">
          </el-table-column>

          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                plain
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                plain
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style:normal;color:red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        :model="editRulesForm"
        :rules="editRulesFormRules"
        ref="editRulesForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="name">
          <el-input
            v-model="editRulesForm.name"
            placeholder="请输入任务名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="desc">
          <el-input
            type="textarea"
            v-model="editRulesForm.desc"
            placeholder="请输入任务描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="任务类型" prop="type_id">
          <el-select
            filterable
            v-model="editRulesForm.type_id"
            placeholder="请选择任务类型"
          >
            <el-option
              v-for="item in typeOptions"
              :key="'任务' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模块类型" prop="module_id">
          <el-select
            filterable
            v-model="editRulesForm.module_id"
            placeholder="请选择模块类型"
          >
            <el-option
              v-for="item in moduleOptions"
              :key="'模块' + item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开放时间" prop="open_day">
          <el-input
            v-model.number="editRulesForm.open_day"
            placeholder="请输入开放时间"
          ></el-input>
        </el-form-item>
        <el-form-item label="条件值" prop="target_limit">
          <el-input
            v-model.number="editRulesForm.target_limit"
            placeholder="请输入条件值"
          ></el-input>
        </el-form-item>
        <el-form-item label="循环周期" prop="cycle_days">
          <el-input
            v-model.number="editRulesForm.cycle_days"
            placeholder="请输入循环周期"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖品ID" prop="reward_id">
          <el-input
            v-model.number="editRulesForm.reward_id"
            placeholder="请输入奖品ID"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖品数量" prop="reward_number">
          <el-input
            v-model.number="editRulesForm.reward_number"
            placeholder="请输入奖品数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖品类型" prop="reward_type">
          <el-radio-group v-model="editRulesForm.reward_type">
            <el-radio :label="1">道具包</el-radio>
            <el-radio :label="2">道具</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="奖品" prop="reward_id" >
                <el-select filterable v-model="editRulesForm.reward_id" placeholder="请选择奖品">
                    <el-option
                    v-for="item in rewardOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item> -->
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  postAddData,
  putEditData,
  deleteData
} from "@/api/newTask/taskList";

export default {
  data() {
    return {
      confirmBtn: false,

      // 要修改的当前行的id
      row_id: 0,

      typeOptions: [],
      moduleOptions: [],

      // 表单规则
      editRulesFormRules: {
        name: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
        desc: [{ required: true, message: "请输入任务描述", trigger: "blur" }],
        type_id: [
          { required: true, message: "请选择任务类型", trigger: "change" }
        ],
        module_id: [
          { required: true, message: "请选择任务模块", trigger: "change" }
        ],
        open_day: [
          { required: true, message: "请输入开放时间", trigger: "blur" }
        ],
        target_limit: [
          { required: true, message: "请输入条件值", trigger: "blur" }
        ],
        cycle_days: [
          { required: true, message: "请输入循环周期", trigger: "blur" }
        ],
        reward_id: [
          { required: true, message: "请选择奖品", trigger: "change" }
        ],
        reward_number: [
          { required: true, message: "请输入奖品数量", trigger: "blur" }
        ]
      },

      editRulesForm: {
        name: undefined,
        desc: undefined,
        type_id: undefined,
        module_id: undefined,
        open_day: undefined,
        target_limit: undefined,
        cycle_days: undefined,
        reward_id: undefined,
        reward_number: undefined,
        reward_type: 1
      },
      dialogStatus: "",
      // dialog的头部信息
      titleMap: {
        update: "编辑",
        create: "添加"
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perpage: 10,
        name: "",
        type: ""
      },

      TaskOptions: [],

      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 点击重置
    handleResetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.type = undefined;
      this.listQuery.name = undefined;

      this.getList();
    },
    getList() {
      this.listLoading = true;

      listData(this.listQuery).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        let arr1 = [];
        for (let key in response.data.header.type_map) {
          arr1.push({
            value: parseInt(key),
            label: response.data.header.type_map[key]
          });
        }
        this.typeOptions = arr1;

        let arr2 = [];
        for (let key in response.data.header.module_map) {
          arr2.push({
            value: parseInt(key),
            label: response.data.header.module_map[key]
          });
        }
        this.moduleOptions = arr2;
        this.TaskOptions = arr2;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    // 点击新增
    handleCreate() {
      this.resetForm("editRulesForm");
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id;
      this.editRulesForm.name = row.name;
      this.editRulesForm.desc = row.desc;
      this.editRulesForm.type_id = row.type_id;
      this.editRulesForm.module_id = row.module_id;
      this.editRulesForm.open_day = row.open_day;
      this.editRulesForm.target_limit = row.target_limit;
      this.editRulesForm.cycle_days = row.cycle_days;
      this.editRulesForm.reward_id = row.reward_id;
      this.editRulesForm.reward_number = row.reward_number;
      this.editRulesForm.reward_type = row.reward_type;

      this.dialogStatus = "update";
      this.dialogFormVisible = true;
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields();
        }
        this.editRulesForm = {
          name: undefined,
          desc: undefined,
          type_id: undefined,
          module_id: undefined,
          open_day: undefined,
          target_limit: undefined,
          cycle_days: undefined,
          reward_id: undefined,
          reward_number: undefined,
          reward_type: 1
        };
      });
    },
    submitForm(formName) {
      if (this.confirmBtn) return;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.confirmBtn = true;
          // alert('submit!');
          if (this.dialogStatus === "create") {
            postAddData(this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList();
                this.dialogFormVisible = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          } else {
            putEditData(this.row_id, this.editRulesForm).then(response => {
              if (response.code === 200) {
                this.getList();
                this.dialogFormVisible = false;
                this.$notify({
                  type: "success",
                  message: "操作成功",
                  position: "bottom-right",
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选要删除的内容",
          type: "warning"
        });
        return;
      } else if (this.multipleSelection.length !== 0) {
        let ids = "";
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`;
        }
        ids = ids.substring(0, ids.length - 1);
        this.dataIds = { ids: ids };
      }
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;

      // this.dataIds)
      deleteData(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
      }
    }
  }
};
</script>

<style scoped></style>
